/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.casestudy1.control;

import com.casestudy1.model.Location;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

/**
 *
 * @author Lenovo
 */
public class GetListLocation {
     public Vector getAtUsername(final String user) {

        Vector list = new Vector();
        try {
            String url = "http://php.yuh351.asia/trafficinfo/getLocationHistory.php?username=" + user;
            Connection conn = Connector.open(url);
            InputStream br = Connector.openInputStream(url);

            int c;
            String text = "";
            
            while ((c = br.read()) != -1) {
                text += (char) c;
            }
            if (text.equals("null")) {
            }
            
            JSONObject jobj = new JSONObject(text);
            
            JSONArray jarr = jobj.getJSONArray("locationhistory");
            JSONObject js;
            
            for (int i = 0; i < jarr.length(); i++) {
                js = jarr.getJSONObject(i);
                Location loc = new Location();
                loc.setAddress(js.getString("address"));
                loc.setLatitude(js.getString("latitude"));
                loc.setLongitude(js.getString("longitude"));
                list.addElement(loc);
            }
            return list;
        } catch (JSONException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return list;
    }
}
